<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\Admin\model\Attr;

class Attribute extends Base
{
	/*属性展示*/
	public function attr_list(){
		$type_id = input('post.type_id');
		$where = '';
		if(!empty($type_id)){
			$where['t.type_id'] = $type_id; 
		}
		$list = Db('attribute')->alias('a')->join('goods_type t','a.type_id = t.type_id')->where($where)->paginate();
		$type_list = Db('goods_type')->select();
		$this->assign('type',$type_list);
		$this->assign('list',$list);
		return $this->fetch();				
	}

	/*属性添加*/
	public function attr_add(){
		$type_list = Db('goods_type')->select();
		$Attr = new Attr();
		if(request()->isPost()){
			$data['attr_name'] = input('post.attr_name');
			$data['type_id'] = input('post.cat_id');
			$data['attr_input_type'] = input('post.attr_input_type');
			$data['attr_group'] = input('post.attr_group');
			$attr_values= input('post.attr_values');
			$keyword_list = trim($attr_values);
			$keyword_arr = explode("\r\n", $keyword_list);
			$data['attr_values'] = implode(',', $keyword_arr);
			$re = $Attr->addData($data);
			$this->redirect('Attribute/attr_list');
		}else{
			$this->assign('type',$type_list);
			return $this->fetch();	
		}
		
	}

	/*属性删除*/
	public function attr_del(){
		$attr_id = input('get.id');
		$result = db('attribute')->where('attr_id',$attr_id)->delete();
		if($result){
			$arr = [
				'code'=>1,
				'msg'=>'删除成功'
			];
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'删除失败'
			];
		}
		echo json_encode($arr);
	}

}